package com.dgut.finaldemo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.finaldemo.entity.Teacher;
import com.dgut.finaldemo.entity.TeacherHealth;
import com.dgut.finaldemo.mapper.TeacherHealthMapper;
import com.dgut.finaldemo.mapper.TeacherMapper;
import com.dgut.finaldemo.vo.DataVO;
import com.dgut.finaldemo.vo.TeacherHealthVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TeacherHealthServiceImpl implements TeacherHealthService {
    @Autowired
    private TeacherHealthMapper teacherHealthMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public DataVO<TeacherHealth> findData(Integer page, Integer limit) {
        DataVO dataVO = new DataVO();
        dataVO.setCode(0);
        dataVO.setMsg("");

        //分页
        IPage<TeacherHealth> healthIPage = new Page<>(page,limit);
        IPage<TeacherHealth> result = teacherHealthMapper.selectPage(healthIPage,null);

        dataVO.setCount(result.getTotal());
        List<TeacherHealth> teacherHealthList = result.getRecords();
        List<TeacherHealthVO> teacherHealthVOList = new ArrayList<>();

        for (TeacherHealth teacherHealth:teacherHealthList){
            TeacherHealthVO teacherHealthVO = new TeacherHealthVO();
            BeanUtils.copyProperties(teacherHealth,teacherHealthVO);
            teacherHealthVOList.add(teacherHealthVO);
        }
        dataVO.setData(teacherHealthVOList);
        return dataVO;
    }

    @Override
    public Integer addTeacherHealth(TeacherHealth teacherHealth) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("username",teacherHealth.getTusername())
                .eq("name",teacherHealth.getTname());
        Teacher teacher = teacherMapper.selectOne(queryWrapper);
        if(teacher != null){
            teacherHealth.setTid(teacher.getTid());
            teacherHealthMapper.insert(teacherHealth);
            return 1;
        }else
            return 0;
    }

    @Override
    public void updateTeacherHealth(TeacherHealth teacherHealth) {
        teacherHealthMapper.updateById(teacherHealth);
    }

    @Override
    public void deleteTeacherHealthById(Integer hid) {
        teacherHealthMapper.deleteById(hid);
    }

    @Override
    public DataVO<TeacherHealth> searchTeacherHealth(Integer page, Integer limit,
                                                     TeacherHealth teacherHealth,
                                                     Float maxTemperature) {
        DataVO dataVO = new DataVO();
        dataVO.setCode(0);
        dataVO.setMsg("");

        if(teacherHealth.getTusername()==null)
            teacherHealth.setTusername("");
        if(teacherHealth.getTname()==null)
            teacherHealth.setTname("");
        if(teacherHealth.getDate()==null)
            teacherHealth.setDate("");
        if(teacherHealth.getArea()==null)
            teacherHealth.setArea("");

        //分页
        IPage<TeacherHealth> healthIPage = new Page<>(page,limit);
        QueryWrapper<TeacherHealth> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .like("tusername",teacherHealth.getTusername())
                .like("tname",teacherHealth.getTname())
                .like("Date",teacherHealth.getDate())
                .like("Area",teacherHealth.getArea());
        if(teacherHealth.getHid()!=null){
            queryWrapper
                    .eq("hid",teacherHealth.getHid());
        }
        if(teacherHealth.getTid()!=null){
            queryWrapper
                    .eq("tid",teacherHealth.getTid());
        }
        if(teacherHealth.getTemperature()!=null&&maxTemperature!=null){
            queryWrapper
                    .between("temperature",teacherHealth.getTemperature(),maxTemperature);
        }else if(teacherHealth.getTemperature()!=null&&maxTemperature==null){
            queryWrapper
                    .ge("temperature",teacherHealth.getTemperature());
        }else if(teacherHealth.getTemperature()==null&&maxTemperature!=null){
            queryWrapper
                    .le("temperature",maxTemperature);
        }
        IPage<TeacherHealth> result =  teacherHealthMapper.selectPage(healthIPage,queryWrapper);

        dataVO.setCount(result.getTotal()); //Long类型

        List<TeacherHealth> teacherHealthList = result.getRecords();
        List<TeacherHealthVO> teacherHealthVOList = new ArrayList<>();
        for (TeacherHealth teacherHealth1 : teacherHealthList){
            TeacherHealthVO teacherHealthVO = new TeacherHealthVO();
            BeanUtils.copyProperties(teacherHealth1, teacherHealthVO);
            teacherHealthVOList.add(teacherHealthVO);
        }
        dataVO.setData(teacherHealthVOList);
        return dataVO;
    }

    @Override
    public DataVO<TeacherHealth> searchTeacherHealthByDay(Integer page, Integer limit) {
        DataVO dataVO = new DataVO();
        dataVO.setCode(0);
        dataVO.setMsg("");
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        System.out.println(date);
        String day = dateFormat.format(date);
        System.out.println(day);
        //分页
        IPage<TeacherHealth> healthIPage = new Page<>(page,limit);
        QueryWrapper<TeacherHealth> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("date",day);
        IPage<TeacherHealth> result =  teacherHealthMapper.selectPage(healthIPage,queryWrapper);

        dataVO.setCount(result.getTotal()); //Long类型

        List<TeacherHealth> teacherHealthList = result.getRecords();
        List<TeacherHealthVO> teacherHealthVOList = new ArrayList<>();
        for (TeacherHealth teacherHealth1 : teacherHealthList){
            TeacherHealthVO teacherHealthVO = new TeacherHealthVO();
            BeanUtils.copyProperties(teacherHealth1, teacherHealthVO);
            teacherHealthVOList.add(teacherHealthVO);
        }
        dataVO.setData(teacherHealthVOList);
        return dataVO;
    }

    @Override
    public TeacherHealth queryTeacherHealthByDate(String username,String date) {
        QueryWrapper<TeacherHealth> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date",date).eq("tusername",username);
        return teacherHealthMapper.selectOne(queryWrapper);
    }
}
